package cn.zyq.ykb.system.pay.core.client.impl.sand;

import cn.hutool.core.date.DateUtil;
import cn.zyq.ykb.system.pay.config.PaySandProperties;
import cn.zyq.ykb.system.pay.core.client.dto.PayOrderUnifiedReqDTO;
import cn.zyq.ykb.system.pay.core.client.dto.PayOrderUnifiedRespDto;
import cn.zyq.ykb.system.pay.core.client.impl.sand.sdk.encrypt.CryptoUtil;
import cn.zyq.ykb.system.utils.DateUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;

/**
 * @Description: 杉德支付（H5）的 PayClient 实现类
 * @Author pengdoudou
 * @Date 2022/8/7 11:50
 */
@Slf4j
public class SandPayClient {

    public static PayOrderUnifiedRespDto doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO){
        PayOrderUnifiedRespDto respDto = null;
        // 组后台报文
        JSONObject head = new JSONObject();
        head.put("version", "1.0");
        head.put("method", PaySandProperties.orderCreateMethod);
        head.put("productId", PaySandProperties.productId);
        head.put("accessType", 1);
        head.put("mid", PaySandProperties.mid);
        head.put("plMid",PaySandProperties.plMid);
        head.put("channelType", "08");
        LocalDateTime now = LocalDateTime.now();
        String reqTime = DateUtils.formatLocalDateTime(now, DateUtils.YYYYMMDDHHMMSS);
        head.put("reqTime", reqTime);

        JSONObject body = new JSONObject();
        body.put("orderCode", reqDTO.getOutTradeNo());
        // body.put("userId", map.get("userId"));
        // body.put("payerName", map.get("payerName"));
        body.put("totalAmount", reqDTO.getAmount()+"");
        body.put("subject", reqDTO.getSubject());
        body.put("body", reqDTO.getBody());
        body.put("txnTimeOut", DateUtil.format(reqDTO.getExpireTime(), "yyyyMMddHHmmss"));
        // body.put("payModeList", map.get("payModeList"));
        body.put("notifyUrl", PaySandProperties.payNotifyUrl);
        body.put("frontUrl", PaySandProperties.frontUrl);
        // body.put("storeId", map.get("storeId"));
        // body.put("terminalId", map.get("terminalId"));
        // body.put("operatorId", map.get("operatorId"));
        // body.put("clearCycle", map.get("clearCycle"));
        // body.put("accountingMode", map.get("accountingMode"));
        // body.put("riskRateInfo", map.get("riskRateInfo"));
        // body.put("bizExtendParams", map.get("bizExtendParams"));
        // body.put("merchExtendParams", map.get("merchExtendParams"));
        // body.put("extend", map.get("extend"));
        JSONObject data = new JSONObject();
        data.put("head", head);
        data.put("body", body);

        try {
            // 报文签名
            String reqSign = CryptoUtil.digitalSign(data.toJSONString());

            JSONObject respJson = new JSONObject();
            respJson.put("data", JSON.toJSONString(data));
            respJson.put("sign", reqSign);
            respDto.setPayReqResp(respJson.toJSONString());
        } catch (Exception e) {
            log.error("杉德支付创建下单参数异常");
            log.error(e.getMessage(), e);
        }
        return respDto;
    }




    /**
     * 签名方法
     *
     * @param data 待签名数据
     * @return
     * @throws Exception
     */
    // public static String digitalSign(String data) throws Exception {
    //     if (null == data) {
    //         return null;
    //     } else {
    //         try {
    //             // 获取密钥配置
    //             String publicKeyPath = DynamicPropertyHelper.getStringProperty("sandpay.public.key", "").get();
    //             String privateKeyPath = DynamicPropertyHelper.getStringProperty("sandpay.private.key", "").get();
    //             String keyPassword = DynamicPropertyHelper.getStringProperty("sandpay.private.key.password", "").get();
    //             // 初始化密钥信息
    //             CertUtil.init(publicKeyPath, privateKeyPath, keyPassword);
    //             byte[] dataBytes = data.getBytes("UTF-8");
    //             // 签名
    //             String signData = new String(Base64.encodeBase64(CryptoUtil.digitalSign(dataBytes, CertUtil.getPrivateKey(), "SHA1WithRSA")), "UTF-8");
    //             log.info("digitalSign(String) =>>sign:{}", signData);
    //             return URLEncoder.encode(signData, "UTF-8");
    //         } catch (Exception var6) {
    //             log.error("digitalSign(String, String)", var6);
    //             throw new Exception("签名异常", var6);
    //         }
    //     }
    // }
}
